package cn.icanci.loopstack.lbrt.core.utils;

import cn.icanci.loopstack.lbrt.core.type.MockBean;

import java.lang.reflect.AnnotatedType;
import java.lang.reflect.ParameterizedType;
import java.util.Arrays;
import java.util.Optional;

/**
 * @author icanci
 * @since 1.0 Created in 2023/06/15 16:54
 */
public class MockBeanUtils {

    /**
     * assembly
     * 
     * @param mockBean mockBean
     * @return Class<?>
     */
    public static Class<?> assembly(MockBean<?> mockBean) {
        Optional<AnnotatedType> optional = Arrays.stream(mockBean.getClass().getAnnotatedInterfaces())
            .filter(f -> f.getType() instanceof ParameterizedType && ((ParameterizedType) f.getType()).getRawType().getTypeName().equals(MockBean.class.getTypeName())).findFirst();
        if (!optional.isPresent()) {
            throw new RuntimeException("MockBean must implements MockBean interface");
        }
        ParameterizedType parameterizedType = (ParameterizedType) optional.get().getType();
        return (Class<?>) parameterizedType.getActualTypeArguments()[0];
    }
}
